package database.service;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import database.auxiliary.LogGenerator;

import po.CatalogPO;

public class CatalogDataServiceSerializableImpl implements CatalogDataService {

	private ArrayList<CatalogPO> catalogList;

	public CatalogDataServiceSerializableImpl() {
		catalogList = new ArrayList<CatalogPO>();
	}

	@Override
	public void load() {
		try {
			File file = new File("data/catalog.ser");
			if (!file.exists()) {
				file.createNewFile();
			}
			ObjectInputStream oiStream = new ObjectInputStream(
					new FileInputStream(file));
			CatalogPO catalogPO;
			while ((catalogPO = (CatalogPO) oiStream.readObject()) != null) {
				catalogList.add(catalogPO);
			}
			LogGenerator.log("Catalog data load succeed");
		} catch (EOFException e) {
			save();
			load();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void save() {
		try {
			FileOutputStream fos = new FileOutputStream("data/catalog.ser");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			for (int i = 0; i < catalogList.size(); i++) {
				oos.writeObject(catalogList.get(i));
			}
			oos.writeObject(null);
			oos.close();
			LogGenerator.log("Catalog data save succeed!");
		} catch (FileNotFoundException e) {
			System.out.println("File found failed!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int insert(CatalogPO catalog) {
		catalog.setId(catalogList.size() + 1);
		catalogList.add(catalog);
		LogGenerator.log("Catalog insert: " + catalog.toString());
		return catalogList.size();
	}

	@Override
	public void delete(CatalogPO catalog) {
		for (int i = 0; i < catalogList.size(); i++) {
			if (catalog.getID() == catalogList.get(i).getID()) {
				catalogList.get(i).setId(-catalogList.get(i).getID());
				LogGenerator.log("Calalog delete: " + catalog);
			}
		}
	}

	@Override
	public void update(CatalogPO catalog) {
		for (int i = 0; i < catalogList.size(); i++) {
			if (catalog.getID() == catalogList.get(i).getID()) {
				catalogList.set(i, catalog);
				LogGenerator.log("Catalog update: " + catalog);
			}
		}
	}

	@Override
	public CatalogPO find(int id) {
		for (int i = 0; i < catalogList.size(); i++) {
			if (id == catalogList.get(i).getID()) {
				LogGenerator.log("Catalog find: " + catalogList.get(i));
				return new CatalogPO(catalogList.get(i));
			}
		}
		return null;
	}

	@Override
	public Iterator<CatalogPO> getIterator() {
		LogGenerator.log("Catalog return itr, length = " + catalogList.size());
		return catalogList.iterator();
	}

//	public static void main(String args[]) {
//		CatalogDataService catalogDataService = new CatalogDataServiceSerializableImpl();
//		catalogDataService.load();
//		Iterator<CatalogPO> caIt = catalogDataService.getIterator();
//		while (caIt.hasNext()) {
//			System.out.println(caIt.next());
//		}
//	}

}
